package cn.s1995.sloth.bms.controller;

import java.util.List;
import java.util.Objects;

import org.jetbrains.annotations.NotNull;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.s1995.sloth.bizs.entity.Shop;
import cn.s1995.sloth.bizs.service.ShopService;
import cn.s1995.sloth.common.core.util.StringUtil;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

/**
 * 店铺
 *
 * @author Lex
 * @date 2023-12-12 08:25:47
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/shop")
@Tag(description = "Shop", name = "BMS-店铺管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ShopController {

    private final ShopService shopService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param req 店铺
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_Shop_view')")
    public R getPage(@ParameterObject Page page, @ParameterObject Shop req) {
        LambdaQueryWrapper<Shop> wrapper = buildQueryWrapper(req);

        return R.ok(shopService.page(page, wrapper));
    }

    @Operation(summary = "列表查询", description = "列表查询")
    @GetMapping("/list")
    // @PreAuthorize("@pms.hasPermission('bizs_Shop_view')")
    public R<List<Shop>> getList(@ParameterObject Shop req) {
        LambdaQueryWrapper<Shop> wrapper = buildQueryWrapper(req);

        return R.ok(shopService.list(wrapper));
    }


    /**
     * 通过id查询店铺
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_Shop_view')")
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(shopService.getById(id));
    }

    /**
     * 新增店铺
     * @param req 店铺
     * @return R
     */
    @Operation(summary = "新增店铺", description = "新增店铺")
    @SysLog("新增店铺")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_Shop_add')")
    public R save(@RequestBody Shop req) {
        return R.ok(shopService.save(req));
    }

    /**
     * 修改店铺
     * @param req 店铺
     * @return R
     */
    @Operation(summary = "修改店铺", description = "修改店铺")
    @SysLog("修改店铺")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_Shop_edit')")
    public R updateById(@RequestBody Shop req) {
        return R.ok(shopService.updateById(req));
    }

    /**
     * 通过id删除店铺
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除店铺", description = "通过id删除店铺")
    @SysLog("通过id删除店铺")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_Shop_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(shopService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_Shop_export')")
    public List<Shop> export(Shop req, Long[] ids) {
		LambdaQueryWrapper<Shop> wrapper = buildQueryWrapper(req);
		wrapper.in(ArrayUtil.isNotEmpty(ids), Shop::getId, ids);
        return shopService.list(wrapper);
    }

    @NotNull
    private static LambdaQueryWrapper<Shop> buildQueryWrapper(Shop req) {
        LambdaQueryWrapper<Shop> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(req.getState()), Shop::getState, req.getState());
        wrapper.eq(StrUtil.isNotBlank(req.getName()), Shop::getName, req.getName());
        wrapper.and(
                StringUtil.isNotEmpty(req.getSearch()),
                w -> w.like(Shop::getId, req.getSearch())
                        .or()
                        .like(Shop::getName, req.getSearch())
                        .or()
                        .like(Shop::getProvince, req.getSearch())
                        .or()
                        .like(Shop::getCity, req.getSearch())
                        .or()
                        .like(Shop::getArea, req.getSearch())
                        .or()
                        .like(Shop::getAddress, req.getSearch())
                        .or()
                        .like(Shop::getContacts, req.getSearch())
                        .or()
                        .like(Shop::getContactPhone, req.getSearch())
                        .or()
                        .like(Shop::getOtherPhone, req.getOtherPhone())
                        .or()
                        .like(Shop::getBoss, req.getSearch())
                        .or()
                        .like(Shop::getBossPhone, req.getSearch())
                        .or()
                        .like(Shop::getLegalPerson, req.getSearch())
                        .or()
                        .like(Shop::getLegalPersonPhone, req.getSearch())
        );
        if(req.hasRangeTime()) {
           	wrapper.between(Shop::getCreateTime, req.getRangeTime()[0], req.getRangeTime()[1]);
        }
        return wrapper;
    }
}
